package com.newsnetwork.article.html;

import com.newsnetwork.api.config.RabbitMQConfig;
//import com.newsnetwork.article.html.controller.ArticleHTMLComponent;
import com.newsnetwork.article.html.controller.ArticleHTMLComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author DingNing
 * @date 2022/11/20 14:59
 * @Description: 消费者
 */
@Component
public class RabbitMQConsumer {
    final static Logger logger = LoggerFactory.getLogger(RabbitMQConsumer.class);

    @Autowired
    private ArticleHTMLComponent articleHTMLComponent;

    @RabbitListener(queues = {RabbitMQConfig.QUEUE_DOWNLOAD_HTML})
    public void watchQueue(String payload, Message message) {
//        System.out.println(payload);

        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        if (routingKey.equalsIgnoreCase("article.download.do")) {
            String articleId = payload.split(",")[0];
            String articleMongoId = payload.split(",")[1];

            try {
                articleHTMLComponent.download(articleId, articleMongoId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.info("html静态页面创建,文章ID:"+articleId);

        } else if (routingKey.equalsIgnoreCase("article.html.download.do")) {

            String articleId = payload;
            try {
                articleHTMLComponent.delete(articleId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.info("html静态页面删除,文章ID:"+articleId);
        } else {
            logger.info("routingKey不符合的规则：" + routingKey);
        }

    }
}
